package com.hmjk.health.home;

import com.hmjk.health.HealthApp;
import com.hmjk.health.R;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class DeviceIDActivity extends Activity implements OnClickListener{
	private HealthApp mApp;
	private String mobile = "", did = "";
	
	private static final String SENT_SMS_CMD_ACTION = "com.hmjk.health.SmsSend";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mApp = (HealthApp) getApplication();
		Intent i = getIntent();
		
		mobile = i.getStringExtra("mobile");
		did = i.getStringExtra("did");
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.deviceid_activity);
		
		ImageButton back = (ImageButton) findViewById(R.id.back);
		back.setOnClickListener(this);
		Button sendSms = (Button)findViewById(R.id.send);
		sendSms.setOnClickListener(this);
		
		TextView mDid = (TextView)findViewById(R.id.devid);
		mDid.setText(did);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction(SENT_SMS_CMD_ACTION);
		registerReceiver(mReceiver, filter);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mReceiver);
		mApp.dismissWaiting();
	}
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if(action.equals(SENT_SMS_CMD_ACTION)){
				int ret = getResultCode();
				
				mApp.dismissWaiting();
				if(ret!=RESULT_OK){
					mApp.showAlertDialog(DeviceIDActivity.this, R.string.sms_failed);
					
				}else {
					mApp.showAlertDialog(DeviceIDActivity.this, getResources().getString(R.string.sms_ok), R.string.ok, 
							new OnClickListener() {
								
								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									finish();
								}
							});

				}
			}
		}
		
	};
	
	private void sendSms(String content, String to){
		SmsManager smsMagager = SmsManager.getDefault();  
        
        Intent i = new Intent(SENT_SMS_CMD_ACTION);  
		
		PendingIntent pi = PendingIntent.getBroadcast(this, 1, i, PendingIntent.FLAG_ONE_SHOT);
        smsMagager.sendTextMessage(to, null, content, pi, null);

        mApp.showWaitting(this, null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.back:{
			finish();
		}break;
		
		case R.id.send:{
			if((mobile == null) || mobile.trim().isEmpty()){
				mApp.showConfirmDialog(this, getResources().getString(R.string.setPhoneFirst), new OnClickListener() {
					
					@Override
					public void onClick(View view) {
						if(view.getId() == R.id.alert_but1){
							setResult(RESULT_OK);
							finish();
						}
					}
				});	
				break;
			}
			mApp.showConfirmDialog(this, getResources().getString(R.string.sms_hint), new OnClickListener() {

				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
					if(view.getId() == R.id.alert_but1){
						String sendString = "<SBHF#" + did + ">";
						sendSms(sendString, mobile);
					}
				}
				
			});
		}break;
		}
	}


}
